package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.DataCenterService;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.datawarning.pojo.dto.DataWarningMessageAddDTO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @ClassName DataCenterController
 * @Description 数据中心
 * @Author mengna.Deng
 * @Date 2021/4/19 9:26
 * @Version 1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/dataCenter")
public class DataCenterController {
    @Resource
    DataCenterService dataCenterService;

    /**
     * 数据预警消息添加
     * @param dataWarningMessageAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/dataWarning/message/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String messageAdd(@RequestBody @Valid DataWarningMessageAddDTO dataWarningMessageAddDTO, BindingResult br) {
        XbbResponse<DataWarningMessageAddDTO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            dataCenterService.dwMessageAdd(dataWarningMessageAddDTO);
            response = new XbbResponse<>();
        }
        return JSON.toJSONString(response);
    }
}
